package com.streambolics.std;

import java.util.Calendar;

public class CalendarFieldExtractor implements FieldExtractor
{
    Calendar _calendar;

    public CalendarFieldExtractor(Calendar aCalendar)
    {
        _calendar = aCalendar;
    }

    @Override
    public String extract (char aCode, int aLen)
    {
        switch (aCode)
        {
            case 'H':
                return extract (Calendar.HOUR_OF_DAY, aLen, 0);
            case 'N':
                return extract (Calendar.MINUTE, aLen, 0);
            case 'S':
                return extract (Calendar.SECOND, aLen, 0);

            case 'Y':
                return extract (Calendar.YEAR, aLen, 0);
            case 'M':
                return extract (Calendar.MONTH, aLen, 1);
            case 'D':
                return extract (Calendar.DAY_OF_MONTH, aLen, 0);
        }

        return "?";
    }

    private String extract (int aField, int aLen, int aOffset)
    {
        int n = _calendar.get (aField) + aOffset;
        String s = "000000000000000000000000000000" + Integer.toString (n);
        return s.substring (s.length () - aLen);
    }
}
